const CryptoBrowser = require('crypto-browserify');
let AESCBC = require('./lib/aescbc');
const Buffer = require('buffer').Buffer;

exports.hexToBase64 = function (str) {
    return str ? Buffer(str, 'hex').toString('base64') : undefined;
};

exports.encrypt = function (data, cypher) {
    // var ivbuf = _sha3(data).slice(0, 16);
    //modified 10-31
    const ivbuf = CryptoBrowser.randomBytes(16);
    if (!Buffer.isBuffer(data)) {
        data = Buffer.from(data);
    }
    return AESCBC.encryptCipherkey(data, cypher, ivbuf);
};

exports.decrypt = function (encryptedData, cypher) {
    let decryptedAES = AESCBC.decryptCipherkey(encryptedData, cypher);
    if (Buffer.isBuffer(decryptedAES)) {
        decryptedAES = decryptedAES.toString();
    }
    return decryptedAES;
};
